package spring;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

public class BeanFactoryTest {
	
	public static void main(String[] args) throws Exception {
		
		DefaultListableBeanFactory beanRegistry = new DefaultListableBeanFactory();
		AbstractBeanDefinition definition = new RootBeanDefinition(Pet.class);
		MutablePropertyValues pvs = definition.getPropertyValues();
		System.out.println(pvs.size());
		System.out.println(pvs);
		pvs.addPropertyValue("id", "1");
		pvs.addPropertyValue("name", "tiger");

		System.out.println(pvs.size());
		System.out.println(pvs);
		
		beanRegistry.registerBeanDefinition("pet",definition);
		BeanFactory container = (BeanFactory)beanRegistry;
		Pet pet = (Pet)container.getBean("pet");
		System.out.println(pet);
		
		//
		loadFromXml();

	}
	
	private static void loadFromXml() {
		DefaultListableBeanFactory beanRegistry = new DefaultListableBeanFactory(); 
		XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanRegistry);
		beanDefinitionReader.loadBeanDefinitions("classpath:simple.xml");
		BeanFactory container = (BeanFactory)beanRegistry;
		Pet pet = (Pet)container.getBean("pet");
		System.out.println(pet);
	}
}
	